package org.example.yiyu.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.yiyu.model.Tag;
import org.example.yiyu.response.BaseResponse;
import org.example.yiyu.service.TagService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/tags")
@RequiredArgsConstructor
public class TagController {

    private final TagService tagService;

    // 创建标签
    @PostMapping
    public BaseResponse<Long> createTag(@RequestBody Tag tag) {

        log.info("新增标签{}",tag.getTagName());
        Long id=tagService.addTag(tag);

        return new BaseResponse<Long>("创建标签成功",true,id);
    }

    // 修改标签
    @PutMapping
    public BaseResponse<String> updateTag(@RequestBody Tag tag) {

        log.info("修改id为{}的标签,修改后名为{}",tag.getId(),tag.getTagName());
        tagService.modifyTag(tag);

        return new BaseResponse<>("修改标签成功",true,null);
    }

    // 获取所有标签
    @GetMapping
    public BaseResponse<List<Tag>> getAllTags() {

        log.info("获取所有标签");

        return new BaseResponse<List<Tag>>("获取所有标签成功",true,tagService.getAllTags());

    }

    // 根据id删除标签（假删除）
    @DeleteMapping("/{id}")
    public BaseResponse<String> deleteTag(@PathVariable Long id) {

        log.info("删除id为{}的标签",id);
        tagService.removeTag(id);
        return new BaseResponse<String>("删除标签成功",true,null);

    }


    // 批量删除某标签下的所有日记
    @DeleteMapping("/batch/{tagId}")
    public BaseResponse<String> deleteDiariesByTag(@PathVariable Long tagId) {

        log.info("批量删除标签id为{}的日记",tagId);
        tagService.deleteDiariesByTag(tagId);

        return new BaseResponse<String>("批量删除标签成功",true,null);
    }
}
